<?php
include_once "BaseM.php";
 
class MemberM extends BaseM{
	
	public $table = "member";
	
	public function __construct() {
		parent::__construct ();
	}
	
	public function insert($obj) {
		$sql = $this->genInserSQL($this->table, $obj);
		$result = @mysql_query($sql) or die ("sql : ".$sql.'\n'.mysql_error ());
		return $result;
	}

	public function update($obj) {	  
		$sql = $this->genUpdateSQL($this->table, $obj, array("id"));
		$result = @mysql_query($sql) or die ("sql : ".$sql.'\n'.mysql_error ());
		return  $result;	
	}
 
	public function delete($key) {
		$sql="delete from $this->table where id = $key";
		$count = @mysql_query( $sql ) or die ("sql : ".$sql.'\n'.mysql_error ());
		return $count;
	}
	
	public function getTableData($key) {
		$sql = "
			select 
				id, login_type, account, password, realname, nickname, sex, is_live, profession_id,
				case login_type when 1 then '本站註冊'
							    when 2 then 'FaceBook 註冊'
							    when 3 then 'G+ 註冊'
							    when 0 then '錯誤'
				end login_type2,
				case profession_id when 0 then '未登記' 
				else ( select name from member_profession where id = member.profession_id ) 
				end profession_name,
				case birthday when 0 then 0 
				else ( date_format(now(), '%Y') - birthday ) 
				end birthday
			from $this->table 
			where id = $key";
		return $this->querySQL($sql, true);
	}
	
	public function queryTable($orderData, $startPage, $pageRow, $searchStr) {
		$whereStr = " is_delete = 0 $searchStr ";
		
		$countSql = "select count(id) as count from $this->table where $whereStr ";
		$count = $this->querySQL($countSql, true);
		
		$resultSql = "
			select 
				id, login_type, profession_id, account, sex, is_live, nickname,
				case login_type when 1 then '本站註冊'
							    when 2 then 'FaceBook 註冊'
							    when 3 then 'G+ 註冊'
							    when 0 then '錯誤'
				end login_type2,
				case sex when 1 then '男'
						 when 2 then '女'
						 when 0 then '未登記'
				end sex2,
				case profession_id when 0 then '未登記' 
				else ( select name from member_profession where id = member.profession_id ) 
				end profession_id_name 
			from $this->table
			where $whereStr
			order by $orderData 
			limit $startPage, $pageRow ";
		$result = $this->querySQL($resultSql);
		
		return array( "count" => $count->count, "result" => $result );
	}

	public function getMemberLogin($account, $password, $registType) {
		
		$sql= "";
		
		switch($registType){
			case 1 :
				$sql = "select * from $this->table where ( account='$account' ) and ( ps_e='$password' ) and regist_type = 1 ";
				break;
			default :
				$sql = "select * from $this->table where ( account='$account' ) and (regist_type = $registType) ";
				break;
		}
		
		return $this->querySQL($sql, true);
	}
	
	public function getAccount($account, $registType){
		$sql = "select * from $this->table where account='$account' and regist_type = $registType ";
		return $this->querySQL($sql, true);
	}
	
	public function insertResigt($obj) {
		$sql = $this->genInserSQL($this->table, $obj);
		@mysql_query($sql) or die ("sql : ".$sql.'\n'.mysql_error ());
		return @mysql_insert_id();
	}
	
	public function updateVerification($account, $ps_e, $device_key) {
		$sql = "update member set email_ver = 1 where (account = '$account') and (ps_e = '$ps_e') and (device_key = '$device_key') ";
		@mysql_query($sql) or die ("sql : ".$sql.'\n'.mysql_error ());
		$sql = "select id from $this->table where (account = '$account') and (ps_e = '$ps_e') and (device_key = '$device_key') and ( email_ver = 1 ) ";
		return $this->querySQL($sql, true)->id > 0 ? true : false ;
	}
	
	public function checkToken($id, $device_key){
		$sql = "select id from $this->table where ( id = $id ) and ( device_key = '$device_key' ) ";
		return $this->querySQL($sql, true) ? true : false ;
	}
	
	public function getCounty($zipCode){
		$sql = "select county from zipcode_tw where ( zip_code = $zipCode ) ";
		$result = $this->querySQL($sql, true);
		return isset($result->county) ? $result->county : "其他" ;
	}

	public function checkLastSendBloodMessage($id){
		$sql = "select id from $this->table where (id = $id) and date_add(now(), interval -".BLOOD_MESSAGE_LENGTH." ".BLOOD_MESSAGE_TIME.") > last_bm_time ";
		return $this->querySQL($sql, true) ? true : false ;
	}
	
	public function updateLastBM($id) {
		$sql = "update member set last_bm_time = now() where (id = $id) ";
		return @mysql_query($sql) or die ("sql : ".$sql.'\n'.mysql_error ());
	}
	
	public function getPushNo($account, $ps_e, $device_key) {
		$sql = "select m_type, push_no from $this->table where (account = '$account') and (ps_e = '$ps_e') and (device_key = '$device_key') ";
		return $this->querySQL($sql, true);
	}
	
}

?>